package cn.edu.sdjzu.xg23.kcsj01_backend.service;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.LiteratureDatabaseAsPaper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.LiteratureDatabaseAsPaperMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.SnowflakeIdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;

@Service
public class LiteratureDatabaseAsPaperService {
    @Autowired
    private LiteratureDatabaseAsPaperMapper literatureDatabaseAsPaperMapper;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    public PageInfo<LiteratureDatabaseAsPaper> get(int pageNum, int pageSize, LiteratureDatabaseAsPaper literatureDatabaseAsPaper) {
        PageHelper.startPage(pageNum, pageSize);
        List<LiteratureDatabaseAsPaper> literatureDatabaseAsPaperList = literatureDatabaseAsPaperMapper.get(literatureDatabaseAsPaper);
        return PageInfo.of(literatureDatabaseAsPaperList);
    }

    @Transactional
    public String save(LiteratureDatabaseAsPaper literatureDatabaseAsPaper) {
        if( literatureDatabaseAsPaper.getId()== null){
            Long id = snowflakeIdGenerator.generateId();
            literatureDatabaseAsPaper.setId(id);
        }

        if (literatureDatabaseAsPaperMapper.save(literatureDatabaseAsPaper)) {
            return "保存成功";
        }
        return "保存失败";
    }

    public String update(LiteratureDatabaseAsPaper literatureDatabaseAsPaper) {
        if (literatureDatabaseAsPaperMapper.update(literatureDatabaseAsPaper)) {
            return "修改成功";
        }
        System.out.println("修改失败");
        return "修改失败";
    }

    public String delete(long id) {
        if (literatureDatabaseAsPaperMapper.delete(id)) {
            return "删除成功";
        }
        return "删除失败";
    }
}
